import axios from "axios";
import { ElMessage } from "element-plus";
import store from "../store";
import router from "@/router/index";

const apiObj = {
  pro: {
    api: "https://mini.chongyeapp.com/api",
  },
  dev: {
    // api: "https://sns.toutiaoke.net.cn/api",
    api: "/api1",
  },
  ws: {
    api: "wss://api.openaier.com/v1/ws",
  },
};
//
const currentObj = apiObj.pro.api; //正式/
// const currentObj = apiObj.dev.api; //測試

const service = axios.create({
  baseURL: currentObj,
  timeout: 60000,
});

// 请求拦截回调
let interceptorsRequest = (config) => {
  // console.log(config);
  // if (
  //   config.url == "/v1/configData" ||
  //   config.url == "/v1/configData?which=2"
  // ) {
  //   config.headers.device = "pc";
  // } else {
  // config.headers.device = "mp";
  // }
  config.headers.device = "pc";
  config.headers.scene = config.scene;
  config.headers.tenant = 0;
  config.headers.safeCode = "";
  config.headers.played = 0;

  // config.headers.token =
  //   "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImlzcyI6Imh0dHBzOi8vc25zLnRvdXRpYW9rZS5uZXQuY24ifQ.eyJpc3MiOiJNci53ZW4iLCJhdWQiOiJodHRwczovL3Nucy50b3V0aWFva2UubmV0LmNuIiwianRpIjoiTXIud2VuIiwic3ViIjoiTXIud2VuIiwiaWF0IjoiMTY4Nzg1MjE2NS42ODIwOTgiLCJuYmYiOiIxNjg3ODUyMTA1LjY4MjA5OCIsImV4cCI6IjE2ODg3MTYxNjUuNjgyMDk4IiwidWlkIjo4fQ.WNz35u-eyOPVQ_FvkOS7q0iKuWqDF_ip2SuQ8T0N8qg";
  // store.state.minToken
  config.headers.token = store.state.minToken;

  // config.headers.token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImlzcyI6Imh0dHBzOi8vc25zLnRvdXRpYW9rZS5uZXQuY24ifQ.eyJpc3MiOiJNci53ZW4iLCJhdWQiOiJodHRwczovL3Nucy50b3V0aWFva2UubmV0LmNuIiwianRpIjoiTXIud2VuIiwic3ViIjoiTXIud2VuIiwiaWF0IjoiMTY4ODAwNzQxOC45NzMyODAiLCJuYmYiOiIxNjg4MDA3MzU4Ljk3MzI4MCIsImV4cCI6IjE2ODg4NzE0MTguOTczMjgwIiwidWlkIjo4fQ.YKn67CgMOewcQ76FggON-R71WWtNNTXgfwdRbyoWUfc"
  // }
  return config;
};

// 请求拦截错误回调
let interceptorsRequestError = (err) => {
  return Promise.reject(err);
};

// 响应拦截回调
let interceptorsResponse = (res) => {
  // 文件下载
  if (res.request.responseURL == "https://api.openaier.com/down") {
    return Promise.resolve(res);
  }
  if (
    res.data.code == 200 ||
    res.data.code == 403021 ||
    res.data.code == 403022 ||
    res.data.code == 416004 ||
    res.data.code == 416003
  ) {
    return Promise.resolve(res.data);
  } else if (res.data.code == "503002") {
    ElMessage.warning("请先进行登录");
    return Promise.reject(res.data);
  } else {
    // ElMessage.error(res.data.message || res.data.data || "server error");
    return Promise.reject(res.data);
  }
};

// 响应拦截错误回调
let interceptorsResponseError = (err) => {
  ElMessage.error(
    err.response.data.content || err.response.data.data || "server error"
  );
  return Promise.reject(err);
};

// 请求拦截
service.interceptors.request.use(interceptorsRequest, interceptorsRequestError);

// 响应拦截
service.interceptors.response.use(
  interceptorsResponse,
  interceptorsResponseError
);

const ajax = (
  url,
  data,
  showLoading = true,
  method = "post",
  headers = { "Access-Control-Allow-Origin": "*" },
  responseType = "json"
) => {
  const config = { url, method, showLoading, headers, responseType };
  method.toUpperCase() === "GET" && (config.params = data);
  method.toUpperCase() === "POST" && (config.data = data);
  return service({ ...config });
};

export { apiObj, service, ajax };
